<?php
namespace app\api\controller;

use think\Cache;
use think\Controller;
use think\Request;
use app\common\model\Order as OrderModel;

class Order extends Controller
{   
    protected $order_model;

    public function _initialize(){
        parent::_initialize();
        $this->order_model = new OrderModel;
    }

    public function place_order($token = 1){
        $ip = getIP();
        if($ip){
            $ipstr = str_replace('.','',$ip);
            if(Cache::has($ipstr)){
                //有缓存
                return json(['status' => -1 , 'msg' => 'Try again in 5 seconds']);
            }
            cache($ipstr,$ipstr,['expire' => 5]);
        }
        if(Request::instance()->isPost()){
            $data = Request::instance()->post();
            $result = $this->order_model->allowField(true)->save($data);
            if($result){
                return json(['status' => 1 , 'msg' => 'place order success']);
            }else{
                return json(['status' => -1 , 'msg' => 'place order error']);
            }
        }
    }
}